package com.wecon.dianping.controller.admin;

import com.wecon.dianping.common.AdminPermission;
import com.wecon.dianping.common.BusinessException;
import com.wecon.dianping.common.EmBusinessError;
import com.wecon.dianping.service.CategoryService;
import com.wecon.dianping.service.SellerService;
import com.wecon.dianping.service.ShopService;
import com.wecon.dianping.service.UserService;
import com.wecon.dianping.util.EncodeByMd5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.controller.admin
 * @description: 后台管理相关操作,
 * 第一个admin代表后台管理模块，第二个admin代表管理员模块
 * @date 2020/3/17 9:29
 */
@RestController
@RequestMapping(value = "/admin/admin")
public class AdminController {

    @Value("${admin.email}")
    private String email;

    @Value("${admin.encryptPassword}")
    private String encryptPassword;

    public static final String CURRENT_ADMIN_SESSION = "currentAdminSession";

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private ShopService shopService;

    /**
     * 管理后台首页
     *
     * @return /
     */
    @RequestMapping(value = "/index")
    @AdminPermission
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("/admin/admin/index");
        Integer countAllUser = userService.countAllUser();
        modelAndView.addObject("userCount", countAllUser);
        modelAndView.addObject("sellerCount", sellerService.countAllSeller());
        modelAndView.addObject("shopCount", shopService.countAllShop());
        modelAndView.addObject("categoryCount", categoryService.countAllCategory());
        modelAndView.addObject("CONTROLLER_NAME", "admin");
        modelAndView.addObject("ACTION_NAME", "index");
        return modelAndView;
    }

    /**
     * 登录页面
     *
     * @return /
     */
    @RequestMapping(value = "/loginPage")
    public ModelAndView loginPage() {
        ModelAndView modelAndView = new ModelAndView("/admin/admin/login");

        return modelAndView;
    }

    /**
     * 后台管理登录
     *
     * @param email    email
     * @param password 密码
     * @return /
     * @throws BusinessException            /
     * @throws UnsupportedEncodingException /
     * @throws NoSuchAlgorithmException     /
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public void login(@RequestParam(name = "email") String email, @RequestParam(name = "password") String password, HttpServletResponse response) throws BusinessException, IOException, NoSuchAlgorithmException {
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, "用户名/密码不能为空");
        }

        if (email.equals(this.email) && EncodeByMd5.encode(password).equals(this.encryptPassword)) {
            // 存在session中
            httpServletRequest.getSession().setAttribute(CURRENT_ADMIN_SESSION, email);
            // SpringBoot项目重定向需要用HttpServletResponse的sendRedirect才能起作用
            response.sendRedirect("/admin/admin/index");

        } else {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, "用户名/密码错误");
        }
    }

}
